from Entities.Calendars.SimpleCalendar import SimpleCalendar
from Entities.Calendars.UnionCalendar import UnionCalendar
from Entities.Calendars.RepeatingHoliday import RepeatingHoliday
from Entities.Calendars.SimpleHoliday import SimpleHoliday
from Entities.Calendars.WeekdayHoliday import WeekdayHoliday

from EntitiesTest.Calendars.CalendarTest import CalendarTest

import datetime
from datetime import timedelta
import unittest

class UnionCalendarTest (CalendarTest):
    
    def setUp (self):
        self.ny  = SimpleCalendar (code='NY',  name='New York', holidays=[WeekdayHoliday.Saturday(), WeekdayHoliday.Sunday(),SimpleHoliday("1",2011,2,21), RepeatingHoliday ("2",2,23)])
        self.lon = SimpleCalendar (code='LON', name='London',   holidays=[WeekdayHoliday.Saturday(), WeekdayHoliday.Sunday(),SimpleHoliday("3",2011,2,22), RepeatingHoliday ("4",2,24)])
        self.cal = UnionCalendar ([self.lon, self.ny])
        
    def testProps (self):
        self.assertEquals (self.cal.code, 'LON NY')
        self.assertEquals (self.cal.name, 'London - New York')
        
    def testWeekends (self):
        oneday = timedelta(days=1)
        when = datetime.date (2011,3,7) # Monday
        for i in range (7):
            self.assert_ (self.cal.isholiday(when) == (i in [5,6]))
            when = when + oneday
            
    def testActuals (self):
        self.assertTrue  (self.cal.isholiday (datetime.date(2011,2,21)))
        self.assertTrue  (self.cal.isholiday (datetime.date(2011,2,22)))   
        self.assertTrue  (self.cal.isholiday (datetime.date(2011,2,23)))   
        self.assertTrue  (self.cal.isholiday (datetime.date(2011,2,24)))   
        self.assertFalse (self.cal.isholiday (datetime.date(2011,2,25)))   
                 
    def testRepeating (self):
        self.assertTrue  (self.cal.isholiday (datetime.date(2011,2,23)))
        self.assertTrue  (self.cal.isholiday (datetime.date(2012,2,24)))
        self.assertFalse (self.cal.isholiday (datetime.date(2011,3,25)))
        self.assertFalse (self.cal.isholiday (datetime.date(2012,3,26)))

def Suite ():
    return unittest.TestLoader().loadTestsFromTestCase(UnionCalendarTest)
        
if __name__ == '__main__':
    unittest.TextTestRunner().run(Suite())
